package ua.kharkov.knure.mikhaylov.st4.web.command;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import ua.kharkov.knure.mikhaylov.st4.db.entities.Account;
import ua.kharkov.knure.mikhaylov.st4.db.entities.Payment;
import ua.kharkov.knure.mikhaylov.st4.db.enums.AccountState;
import ua.kharkov.knure.mikhaylov.st4.db.enums.PaymentState;
import ua.kharkov.knure.mikhaylov.st4.db.management.DBManager;
import ua.kharkov.knure.mikhaylov.st4.settings.LocaleData;
import ua.kharkov.knure.mikhaylov.st4.settings.Path;

/**
 * Command - finally creates the payment
 * 
 * @author Saint
 * 
 */
public class CreatePaymentCommand extends Command {

	private static final long serialVersionUID = -5655495526322575130L;
	private static final Logger log = Logger
			.getLogger(CreatePaymentCommand.class);

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		log.debug("Command starts");

		log.trace("Checking inserting parameters");

		HttpSession session = request.getSession();

		if (session.getAttribute("targetUser") == null
				|| session.getAttribute("senderAccount") == null
				|| session.getAttribute("recieverAccount") == null
				|| session.getAttribute("value") == null) {
			String errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.attribure.extraction");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return Path.PAGE_ERROR;
		}
		if ((Double) session.getAttribute("value") <= 0) {
			String errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.payment.value");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return Path.PAGE_ERROR;
		}
		Account senderAccount = (Account) session.getAttribute("senderAccount");
		Account recieverAccount = (Account) session
				.getAttribute("recieverAccount");
		if (senderAccount.getAccountState() == AccountState.CLOSED
				|| recieverAccount.getAccountState() == AccountState.CLOSED) {
			String errorMessage = LocaleData.getResourceBundle().getString(
					"servlet.error.closedaccountpayment");
			request.setAttribute("errorMessage", errorMessage);
			log.error("errorMessage --> " + errorMessage);
			return Path.PAGE_ERROR;
		}
		double value = (Double) session.getAttribute("value");

		session.setAttribute("targetUser", null);
		session.setAttribute("senderAccounts", null);
		session.setAttribute("senderAccount", null);
		session.setAttribute("recieverAccounts", null);
		session.setAttribute("recieverAccount", null);
		session.setAttribute("value", null);

		log.debug("Creating a payment");

		Payment payment = new Payment();
		payment.setPayer(senderAccount);
		payment.setReciever(recieverAccount);
		payment.setVal(value);
		payment.setPaymentDate(new java.sql.Date((new java.util.Date())
				.getTime()));
		payment.setPaymentState(PaymentState.OPEN);

		// we dont need to set id here

		DBManager.getInstance().insertPayment(payment);

		log.debug("Payment saved");

		request.setAttribute("message", LocaleData.getResourceBundle()
				.getString("servlet.success.payment.ask"));
		request.setAttribute("next", Path.COMMAND_BROWSE_USER_PAGE);
		String forward = Path.PAGE_SUCCESS;

		log.debug("Command finished");
		return forward;
	}

}
